import { fromJS } from 'immutable';
import { REPORT_ERROR, CLEAN_ERROR } from '../actions/error';
import createReducer from '../utils/createReducer';

const initialState = fromJS({
  title: null,
  type: null,
  messages: [],
});

const handlers = {
  [REPORT_ERROR](state, { payload }) {
    const { title, type, messages } = payload;

    return state.withMutations((s) => {
      s.set('title', title);
      s.set('type', type);
      s.set('messages', fromJS(messages));
    });
  },

  [CLEAN_ERROR](state) {
    return state.withMutations((s) => {
      s.set('title', null);
      s.set('type', null);
      s.set('messages', fromJS([]));
    });
  },
};

export default createReducer(initialState, handlers);
